package back;

import core.Comment;
import core.TestData;
import core.User;
import dao.IComment;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author Michal Horak
 */
@ManagedBean(name = "comment")
@ApplicationScoped
public class CommentBean implements Serializable {

    @EJB
    IComment u;
    private Comment comment = null;
    private List<SelectItem> allComments;
    private List<Comment> selectedComments;
    private String content;

    public CommentBean() {
    }

    public List<Comment> getAllComments() {
        return u.findAllComments();
    }

    public List<SelectItem> getAllCommentsItemList() {
        allComments = new ArrayList<SelectItem>();
        for (Iterator<Comment> it = u.findAllComments().iterator(); it.hasNext();) {
            Comment e = it.next();
            allComments.add(new SelectItem(e, e.getTitle()));
        }
        return allComments;
    }

    public List<Comment> getSelectedComments() {
        return selectedComments;
    }

    public void setSelectedComments(List<Comment> selectedComments) {
        this.selectedComments = selectedComments;
    }

    public List<Comment> selectedCommentsFromUser(User id) {
        return u.findAllCommentsFromUser(id);
    }

    public Comment getComment() {
        return comment;
    }

    public void setComment(Comment user) {
        this.comment = user;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Comment findComment(String username) {
        return u.findComment(username);
    }

    public int findCommentId(String username) {
        return u.findComment(username).getId();
    }

    public String saveComment() {

        if (TestData.loggedUser != null) {

            this.comment = new Comment(TestData.COMM_TABLE.size() + 1, "nazev", content, TestData.loggedUser.getId());
            u.createComment(comment);

            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Komentář byl uložen "));
        }

        return "#";

    }

    public String editComment(Comment user) {
        this.comment = user;
        return "user";
    }

    private UIComponent findComponent(UIComponent parent, String id) {
        if (id.equals(parent.getId())) {
            return parent;
        }
        Iterator<UIComponent> kids = parent.getFacetsAndChildren();
        while (kids.hasNext()) {
            UIComponent kid = kids.next();
            UIComponent found = findComponent(kid, id);
            if (found != null) {
                return found;
            }
        }
        return null;
    }
}
